package com.itheima.d2_xml;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import javax.xml.parsers.SAXParser;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/*
     目标：掌握使用Dom4j框架解析XML文件。
 */
public class Dom4JTest1 {
    public static void main(String[] args) throws Exception {

        SAXReader saxReader = new SAXReader();
        Document doument = saxReader.read(new File("rtx.xml"));
        Element root = doument.getRootElement();
        List<Element> elements = root.elements();
        for (Element element : elements) {
            Attribute idAttr = element.attribute("id");
            String id = idAttr.getValue();

            Element nameElement = element.element("name");
            String name = nameElement.getText();
            Element sexElement = element.element("sex");
            String sex = sexElement.getText();
            Element addressElement = element.element("地址");
            String addres = addressElement.getText();
            Element passwdElement = element.element("password");
            String passwd = passwdElement.getText();

//            System.out.println(id + " " + name + " " +sex + " " +addres+ " " +passwd);
            System.out.printf("id=%s name=%s sex=%s addres=%s passwd =%s",id,name,sex,addres,passwd);
        }


//        SAXReader saxReader = new SAXReader();
//
//        Document document =
//                saxReader.read("student.xml");
//
//        Element root = document.getRootElement();
//        System.out.println(root.getName());
//
//        List<Element> elements = root.elements();
//        for (Element element : elements) {
//            System.out.println(element.getName());
//        }
    }
}
